home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / PSDraw.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  4.8 KB  |  195 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: PSDraw.py,v 1.1.1.1 1998/08/18 13:07:55 sjoerd Exp $
  4. #
  5. # simple postscript graphics interface
  6. #
  7. # History:
  8. #    96-04-20 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. import EpsImagePlugin
  17. import string
  18.  
  19. class PSDraw:
  20.     def __init__(self, fp = None):
  21.     if not fp:
  22.         import sys
  23.         fp = sys.stdout
  24.     self.fp = fp
  25.  
  26.     def begin_document(self, id = None):
  27.     "Write Postscript DSC header"
  28.     # FIXME: incomplete
  29.     self.fp.write("%!PS-Adobe-3.0\n"
  30.               "save\n"
  31.               "/showpage { } def\n"
  32.               "%%EndComments\n"
  33.               "%%BeginDocument\n")
  34.     #self.fp.write(ERROR_PS) # debugging!
  35.     self.fp.write(EDROFF_PS)
  36.     self.fp.write(VDI_PS)
  37.     self.fp.write("%%EndProlog\n")
  38.     self.isofont = {}
  39.  
  40.     def end_document(self):
  41.     "Write Postscript DSC footer"
  42.     self.fp.write("%%EndDocument\n"
  43.               "restore showpage\n"
  44.               "%%End\n")
  45.     if hasattr(self.fp, "flush"):
  46.         self.fp.flush()
  47.  
  48.     def setfont(self, font, size):
  49.     if not self.isofont.has_key(font):
  50.         # reencode font
  51.         self.fp.write("/PSDraw-%s ISOLatin1Encoding /%s E\n" %\
  52.               (font, font))
  53.         self.isofont[font] = 1
  54.     # rough
  55.     self.fp.write("/F0 %d /PSDraw-%s F\n" % (size, font))
  56.  
  57.     def setink(self, ink):
  58.     print "*** NOT YET IMPLEMENTED ***"
  59.  
  60.     def line(self, xy0, xy1):
  61.     xy = xy0 + xy1
  62.     self.fp.write("%d %d %d %d Vl\n" % xy)
  63.  
  64.     def rectangle(self, box):
  65.     self.fp.write("%d %d M %d %d 0 Vr\n" % box)
  66.  
  67.     def text(self, xy, text):
  68.     text = string.joinfields(string.splitfields(text, "("), "\\(")
  69.     text = string.joinfields(string.splitfields(text, ")"), "\\)")
  70.     xy = xy + (text,)
  71.     self.fp.write("%d %d M (%s) S\n" % xy)
  72.  
  73.     def image(self, box, im, dpi = None):
  74.     "Write an PIL image"
  75.     # default resolution depends on mode
  76.     if not dpi:
  77.         if im.mode == "1":
  78.         dpi = 200 # fax
  79.         else:
  80.         dpi = 100 # greyscale
  81.     # image size (on paper)
  82.     x = float(im.size[0] * 72) / dpi
  83.     y = float(im.size[1] * 72) / dpi
  84.     # max allowed size
  85.     xmax = box[2] - box[0]
  86.     ymax = box[3] - box[1]
  87.     if x > xmax:
  88.         y = y * xmax / x; x = xmax
  89.     if y > ymax:
  90.         x = x * ymax / y; y = ymax
  91.     dx = (xmax - x) / 2 + box[0]
  92.     dy = (ymax - y) / 2 + box[1]
  93.     self.fp.write("%f %f translate\n" % (dx, dy))
  94.     if (x, y) != im.size:
  95.         # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
  96.         sx = x / im.size[0]
  97.         sy = y / im.size[1]
  98.         self.fp.write("%f %f scale\n" % (sx, sy))
  99.     EpsImagePlugin._save(im, self.fp, None, 0)
  100.     self.fp.write("\n")
  101.  
  102. # --------------------------------------------------------------------
  103. # Postscript driver
  104.  
  105. #
  106. # EDROFF.PS -- Postscript driver for Edroff 2
  107. #
  108. # History:
  109. # 94-01-25 fl: created (edroff 2.04)
  110. #
  111. # Copyright (c) Fredrik Lundh 1994.
  112. #
  113.  
  114. EDROFF_PS = """\
  115. /S { show } bind def
  116. /P { moveto show } bind def 
  117. /M { moveto } bind def 
  118. /X { 0 rmoveto } bind def 
  119. /Y { 0 exch rmoveto } bind def 
  120. /E {    findfont
  121.     dup maxlength dict begin
  122.     {
  123.         1 index /FID ne { def } { pop pop } ifelse
  124.     } forall
  125.     /Encoding exch def
  126.     dup /FontName exch def
  127.     currentdict end definefont pop
  128. } bind def
  129. /F {    findfont exch scalefont dup setfont
  130.     [ exch /setfont cvx ] cvx bind def
  131. } bind def
  132. """
  133.  
  134. #
  135. # VDI.PS -- Postscript driver for VDI meta commands
  136. #
  137. # History:
  138. # 94-01-25 fl: created (edroff 2.04)
  139. #
  140. # Copyright (c) Fredrik Lundh 1994.
  141. #
  142.  
  143. VDI_PS = """\
  144. /Vm { moveto } bind def
  145. /Va { newpath arcn stroke } bind def
  146. /Vl { moveto lineto stroke } bind def
  147. /Vc { newpath 0 360 arc closepath } bind def
  148. /Vr {    exch dup 0 rlineto
  149.     exch dup neg 0 exch rlineto
  150.     exch neg 0 rlineto
  151.     0 exch rlineto
  152.     100 div setgray fill 0 setgray } bind def
  153. /Tm matrix def
  154. /Ve {    Tm currentmatrix pop
  155.     translate scale newpath 0 0 .5 0 360 arc closepath
  156.     Tm setmatrix
  157. } bind def
  158. /Vf { currentgray exch setgray fill setgray } bind def
  159. """
  160.  
  161. #
  162. # ERROR.PS -- Error handler
  163. #
  164. # History:
  165. # 89-11-21 fl: created (pslist 1.10)
  166. #
  167.  
  168. ERROR_PS = """\
  169. /landscape false def
  170. /errorBUF 200 string def
  171. /errorNL { currentpoint 10 sub exch pop 72 exch moveto } def
  172. errordict begin /handleerror {
  173.     initmatrix /Courier findfont 10 scalefont setfont
  174.     newpath 72 720 moveto $error begin /newerror false def
  175.     (PostScript Error) show errorNL errorNL
  176.     (Error: ) show
  177.         /errorname load errorBUF cvs show errorNL errorNL
  178.     (Command: ) show
  179.         /command load dup type /stringtype ne { errorBUF cvs } if show
  180.         errorNL errorNL
  181.     (VMstatus: ) show
  182.         vmstatus errorBUF cvs show ( bytes available, ) show 
  183.         errorBUF cvs show ( bytes used at level ) show
  184.         errorBUF cvs show errorNL errorNL
  185.     (Operand stargck: ) show errorNL /ostargck load { 
  186.         dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  187.     } forall errorNL
  188.     (Execution stargck: ) show errorNL /estargck load {
  189.         dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  190.     } forall
  191.     end showpage
  192. } def end
  193. """
  194.  
  195.